﻿using System;
using System.Collections.Generic;
using System.Text;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security.Authorization;
using System.Data;
using System.Data.Common;

namespace Sinacor.Servico.Bovespa.Usuario.ResourceAccess
{
    public class Usuario : BaseResourceAccess
    {
        #region Construtor
        
        public Usuario(): base(AuthorizationContext.Current.CompanyId) 
        {
        
        }

        #endregion

        #region ConsultarUsuarios

        public List<BusinessEntity.Usuario> ConsultarUsuarios(BusinessEntity.Parameter.FiltroUsuario request)
        {
            List<BusinessEntity.Usuario> response = new List<BusinessEntity.Usuario>();

            using (DbCommand command = this.DataBase.GetSqlStringCommand(" "))
            {
                String condicao = "";

                if (!String.IsNullOrEmpty(request.Nome))
                {
                    this.DataBase.AddInParameter(command, "Nome", DbType.String, request.Nome);
                    condicao = " AND USUARIO.NM_USUARIO LIKE :Nome ";
                }

                if(request.Codigo.HasValue)
                {
                    this.DataBase.AddInParameter(command, "Codigo", DbType.Int32, request.Codigo);
                    condicao += " AND USUARIO.CD_USUARIO = :Codigo ";
                }

                if (!String.IsNullOrEmpty(request.Login))
                {
                    this.DataBase.AddInParameter(command, "Login", DbType.String, request.Login);
                    condicao += " AND USUARIO.CD_LOGIN LIKE :Login ";
                }

                if (!request.Operador)
                    condicao += " AND USUARIO.CD_USUARIO NOT IN (SELECT CD_OPERADOR FROM TOROPERA) ";

                if (request.Bloqueado.HasValue)
                {
                    if (request.Bloqueado.Value)
                        condicao += " AND USUARIO.IN_BLOQUEIO = 'S' ";
                    else
                        condicao += " AND USUARIO.IN_BLOQUEIO = 'N' ";
                }

                this.DataBase.AddInParameter(command, "CodigoEmpresa", DbType.Int32, AuthorizationContext.Current.CompanyId);

                //StringBuilder sql = new StringBuilder();

                String sql = String.Format(@"SELECT USUARIO.CD_USUARIO  AS Codigo,
                                                  USUARIO.NM_USUARIO  AS Nome,
                                                  USUARIO.CD_LOGIN    AS Login,
                                                  USUARIO.CD_EMPRESA  AS Corretora,
                                                  USUARIO.IN_BLOQUEIO AS Bloqueado
                                             FROM TGEUSUARIO USUARIO,
                                                  TGEUSUEMP USUARIOXEMP
                                            WHERE USUARIO.CD_USUARIO = USUARIOXEMP.CD_USUARIO(+)
                                              AND USUARIOXEMP.CD_EMPRESA = :CodigoEmpresa
                                                  {0}
                                         ORDER BY USUARIO.NM_USUARIO", condicao);

                command.CommandText = sql;

                response = this.FindAll<BusinessEntity.Usuario>(command,
                    reader =>
                    {
                        BusinessEntity.Usuario entity = new BusinessEntity.Usuario();

                        entity.Codigo = reader.GetValue<Int32>("Codigo");
                        entity.Nome = reader.GetValue<String>("Nome");
                        entity.Login = reader.GetValue<String>("Login");
                        entity.Corretora = reader.GetValue<Int32?>("Corretora");
                        entity.Bloqueado = reader.GetValue<String>("Bloqueado").Equals("S");

                        return entity;
                    });

                return response;
            }
        }

        #endregion
    }
}
